package board;

import flixel.FlxSprite;
import flixel.FlxG;
import flixel.system.FlxSound;
import flixel.util.FlxRandom;

/**
 * ...
 * @author ...
 */
class Dice extends FlxSprite
{
	private var rolls:Int;
	private var lastRoll:Float;
	public var rolling:Bool; 
	private var dadoSound:FlxSound;
	public function new(X:Float = 0, Y:Float = 0)
	{
		super(X, Y);
		rolls = 0;
		lastRoll = 0;
		this.loadGraphic("assets/images/dados.png", true, 130, 150);
		this.animation.add("1",[0],30,false);
		this.animation.add("2",[1],30,false);
		this.animation.add("3",[2],30,false);
		this.animation.add("4",[3],30,false);
		this.animation.add("5",[4],30,false);
		this.animation.add("6", [5], 30, false);
		rolling = false;
		
		dadoSound = new FlxSound();
		dadoSound.loadEmbedded("assets/sounds/dado.wav");
	}
	
	public function roll():Void
	{
		rolls = 10;
		lastRoll = 0.15;
		rolling = true;
	}
	
	public function value():Int
	{
		return this.animation.frameIndex + 1;
	}
	
	override function update():Void
	{
		super.update();
		lastRoll -= FlxG.elapsed;
		if (rolls > 0 && lastRoll <= 0)
		{
			if (dadoSound.playing)
				dadoSound.stop();
			dadoSound.volume = 0.5;
			dadoSound.play();
			lastRoll = 0.15;
			rolls--;
			var rand:Int = this.animation.frameIndex;
			while (rand == this.animation.frameIndex)
				rand = FlxRandom.intRanged(0, 5);
			this.animation.frameIndex = rand;
			if (rolls == 0)
				rolling = false;
		}
	}
	
}